//
//  WelComeViewController.swift
//  Swiftsweibo
//
//  Created by 张鹏飞 on 16/1/9.
//  Copyright © 2016年 张鹏飞. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WelComeViewController: UIViewController {

/// 设置界面视图层次结构
    override func loadView() {
        view = backImageView
        setUpUI()
    }
    
//视图已经显示,通常用来处理动画/键盘
    override func viewDidAppear(animated: Bool) {
        //1.更新约束 
        iconView.snp_updateConstraints { (make) -> Void in
            make.centerY.equalTo(backImageView.snp_bottom).offset(-view.bounds.height + 200)
        }
        //2.动画
        welcomeLabel.alpha = 0
        UIView.animateWithDuration(1.0,
            delay: 0,
            usingSpringWithDamping: 0.8,
            initialSpringVelocity: 10,
            options: [],
            animations: { () -> Void in
                //修改所有'可动画'属性
                self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.3,
                    animations: { () -> Void in
                        self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        print("OK")
                        
                        //发送通知
                        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNotification, object: nil)
                })
        }
    }
    //视图加载完成后续处理通常用来处理数据
    override func viewDidLoad() {
        super.viewDidLoad()

        // 异步加载用户头像
        iconView.sd_setImageWithURL(UserAccountViewModel.sharedUserAccount.avatar, placeholderImage: UIImage(named: "avatar_default_big"))
    }
    
    // MARK: - 懒加载 要加载的控件
/// 背景图像
    private lazy var backImageView: UIImageView = UIImageView(imageName: "ad_background")
/// 默认头像
    private lazy var iconView: UIImageView = {
        
        let iv = UIImageView(imageName: "avatar_default_big")
        iv.layer.cornerRadius = 45
        iv.layer.masksToBounds = true
        return iv
        
    }()
/// 欢迎书签
    private lazy var welcomeLabel: UILabel = {
    
        let wl = UILabel(title: "WellCome,Back", fontSize: 18, color: UIColor.darkGrayColor())
        
        return wl
    }()
    

  }

// MARK: - 设置界面
extension WelComeViewController {

    private func setUpUI() {
    
//        view.addSubview(backImageView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
/// 设置布局
      iconView.snp_makeConstraints { (make) -> Void in
        make.centerX.equalTo(backImageView.snp_centerX)
        make.centerY.equalTo(backImageView.snp_bottom).offset(-200)
        make.width.equalTo(90)
        make.height.equalTo(90)
        }
        
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
    }
}
